<template>
  <div class="dashboard">
    <el-row :gutter="20">
      <el-col :span="6">
        <el-card shadow="hover">
          <template #header>
            <div class="card-header">
              <span>论文题目</span>
              <el-tag>{{ stats.topics }}</el-tag>
            </div>
          </template>
          <div class="card-content">
            <el-icon><Document /></el-icon>
            <span>总题目数</span>
          </div>
        </el-card>
      </el-col>

      <el-col :span="6">
        <el-card shadow="hover">
          <template #header>
            <div class="card-header">
              <span>选题情况</span>
              <el-tag>{{ stats.selections }}</el-tag>
            </div>
          </template>
          <div class="card-content">
            <el-icon><Select /></el-icon>
            <span>已选题数</span>
          </div>
        </el-card>
      </el-col>

      <el-col :span="6">
        <el-card shadow="hover">
          <template #header>
            <div class="card-header">
              <span>开题报告</span>
              <el-tag>{{ stats.reports }}</el-tag>
            </div>
          </template>
          <div class="card-content">
            <el-icon><Document /></el-icon>
            <span>已提交数</span>
          </div>
        </el-card>
      </el-col>

      <el-col :span="6">
        <el-card shadow="hover">
          <template #header>
            <div class="card-header">
              <span>论文文档</span>
              <el-tag>{{ stats.documents }}</el-tag>
            </div>
          </template>
          <div class="card-content">
            <el-icon><Files /></el-icon>
            <span>已提交数</span>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <el-row :gutter="20" class="mt-20">
      <el-col :span="12">
        <el-card>
          <template #header>
            <div class="card-header">
              <span>最近选题</span>
            </div>
          </template>
          <el-table :data="recentSelections" stripe>
            <el-table-column prop="student.user.real_name" label="学生" />
            <el-table-column prop="thesisTopic.title" label="题目" />
            <el-table-column prop="status" label="状态">
              <template #default="{ row }">
                <el-tag :type="getStatusType(row.status)">
                  {{ getStatusText(row.status) }}
                </el-tag>
              </template>
            </el-table-column>
            <el-table-column prop="apply_time" label="申请时间" />
          </el-table>
        </el-card>
      </el-col>

      <el-col :span="12">
        <el-card>
          <template #header>
            <div class="card-header">
              <span>最近指导记录</span>
            </div>
          </template>
          <el-table :data="recentGuidance" stripe>
            <el-table-column prop="topicSelection.student.user.real_name" label="学生" />
            <el-table-column prop="content" label="内容" show-overflow-tooltip />
            <el-table-column prop="record_time" label="记录时间" />
          </el-table>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'

const stats = ref({
  topics: 0,
  selections: 0,
  reports: 0,
  documents: 0
})

const recentSelections = ref([])
const recentGuidance = ref([])

const getStatusType = (status) => {
  const types = {
    pending: 'warning',
    approved: 'success',
    rejected: 'danger'
  }
  return types[status] || 'info'
}

const getStatusText = (status) => {
  const texts = {
    pending: '待审核',
    approved: '已通过',
    rejected: '已拒绝'
  }
  return texts[status] || status
}

const fetchDashboardData = async () => {
  try {
    const [topicsRes, selectionsRes, reportsRes, documentsRes] = await Promise.all([
      fetch('/api/thesis-topics'),
      fetch('/api/topic-selections'),
      fetch('/api/proposal-reports'),
      fetch('/api/thesis-documents')
    ])

    const [topics, selections, reports, documents] = await Promise.all([
      topicsRes.json(),
      selectionsRes.json(),
      reportsRes.json(),
      documentsRes.json()
    ])

    stats.value = {
      topics: topics.length,
      selections: selections.length,
      reports: reports.length,
      documents: documents.length
    }

    recentSelections.value = selections.slice(0, 5)
  } catch (error) {
    ElMessage.error('获取数据失败')
  }
}

const fetchRecentGuidance = async () => {
  try {
    const response = await fetch('/api/guidance-records')
    const data = await response.json()
    recentGuidance.value = data.slice(0, 5)
  } catch (error) {
    ElMessage.error('获取指导记录失败')
  }
}

onMounted(() => {
  fetchDashboardData()
  fetchRecentGuidance()
})
</script>

<style scoped>
.dashboard {
  padding: 20px;
}

.mt-20 {
  margin-top: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.card-content {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 16px;
}

.el-icon {
  font-size: 24px;
}
</style> 